package org.middleware.controller.system;

import lombok.extern.slf4j.Slf4j;
import org.middleware.common.AuthenticationToken.RequireLogin;
import org.middleware.common.BusinessException;
import org.middleware.controller.common.CommonHttpResponse;
import org.middleware.entity.system.SysDepartments;
import org.middleware.service.system.SysDepartmentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequireLogin
public class SysDepartmentsController {
    @Autowired
    private SysDepartmentsService departmentsService;

    @GetMapping("/groups/enterprise/{enterpriseId}")
    public CommonHttpResponse getDepartmentsByEnterprise(
            @PathVariable("enterpriseId") int enterpriseId, @RequestAttribute("isAdmin") int isAdmin) {
        try {
            List<SysDepartments> result = departmentsService.getDepartmentByEnterpriseId(enterpriseId, isAdmin);
            return CommonHttpResponse.success(result);
        } catch (Exception e) {
            log.error("根据企业id查询部门信息异常", e);
            return CommonHttpResponse.fail(500, "查询部门信息异常，请联系管理员");
        }
    }

    @GetMapping("/departments/enterprises/{enterprise}/tree")
    public CommonHttpResponse getDepartmentTree(@PathVariable int enterprise) {
        try{
            List<SysDepartments> result = departmentsService.getDepartmentTree(enterprise);
            return CommonHttpResponse.success(result);
        } catch (BusinessException e) {
            return CommonHttpResponse.fail(400, e.getMessage());
        } catch (Exception e) {
            log.error("查询部门树异常：", e);
            return CommonHttpResponse.fail(500, "查询部门树异常,请联系管理员");
        }
    }

    @PostMapping("/departments")
    public CommonHttpResponse create(@RequestBody SysDepartments department) {
        try {
            departmentsService.create(department);
            return CommonHttpResponse.success("部门新增成功");
        } catch (BusinessException e) {
            return CommonHttpResponse.fail(400, e.getMessage());
        } catch (Exception e) {
            log.error("新增部门信息异常:", e);
            return CommonHttpResponse.fail(500, "部门新增失败，请联系管理员");
        }
    }

    @PutMapping("/departments")
    public CommonHttpResponse update(@RequestBody SysDepartments department) {
        try {
            departmentsService.update(department);
            return CommonHttpResponse.success("修改成功");
        } catch (BusinessException e) {
            return CommonHttpResponse.fail(400, "部门信息修改失败");
        } catch (Exception e) {
            log.error("修改部门信息异常:", e);
            return CommonHttpResponse.fail(500, "部门信息修改失败，请联系管理员");
        }
    }

    @DeleteMapping("/departments/{id}")
    public CommonHttpResponse deleteWithChildren(@PathVariable int id) {
        try {
            departmentsService.deleteWithDownDepartment(id);
            return CommonHttpResponse.success("删除成功");
        } catch (BusinessException e) {
            return CommonHttpResponse.fail(400, e.getMessage());
        } catch (Exception e) {
            log.error("删除部门信息异常:", e);
            return CommonHttpResponse.fail(500, "删除部门失败，请联系管理员");
        }
    }
}
